﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.MQBase
{
    /// <summary>
    /// MQ插件管理器
    /// </summary>
    public class MQMessageQueueFactoryManager
    {
        private static readonly Dictionary<string, IMQMessageQueueFactory> _factoryDic;

        static MQMessageQueueFactoryManager()
        {
            _factoryDic = new Dictionary<string, IMQMessageQueueFactory>();
        }

        /// <summary>
        /// 添加工厂对象
        /// </summary>
        /// <param name="fatcory">工厂对象</param>
        /// <exception cref="ArgumentNullException">工厂对象为null</exception>
        public static void AddFatcory(IMQMessageQueueFactory fatcory)
        {
            if (fatcory == null)
            {
                throw new ArgumentNullException(nameof(fatcory));
            }

            if (string.IsNullOrWhiteSpace(fatcory.Name))
            {
                throw new ArgumentNullException(nameof(fatcory.Name));
            }

            _factoryDic.Add(fatcory.Name, fatcory);
        }

        /// <summary>
        /// 根据消息队列工厂名称获取消息队列工厂
        /// </summary>
        /// <param name="name">消息队列工厂名称</param>
        /// <returns>消息队列工厂</returns>
        public static IMQMessageQueueFactory GetFatcoryByName(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            IMQMessageQueueFactory fatcory;
            if (_factoryDic.TryGetValue(name, out fatcory))
            {
                return fatcory;
            }

            return null;
        }
    }
}
